package com.qffx.moviebase.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.qffx.moviebase.R;
import com.qffx.moviebase.bean.RecommendADEntity;
import com.qffx.moviebase.bean.RecommendEntity;
import com.qffx.moviebase.bean.RecommendImpl;
import com.qffx.moviebase.bean.RecommendInfo1Entity;
import com.qffx.moviebase.bean.RecommendInfo2Entity;
import com.squareup.picasso.Picasso;

import java.util.Calendar;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCMediaManager;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * Created by 韩爽 on 2017/2/8.
 */

public class RecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private RecommendEntity recommendEntity;
    private Context context;
    private Calendar calendar;
    public static final String TAG="RecommendAdapter";
    //多布局
    public static final int TYPE_AD=0;
    public static final int TYPE_1=1;
    public static final int TYPE_2=2;

    public RecommendAdapter(RecommendEntity recommendEntity, Context context) {
        this.recommendEntity = recommendEntity;
        this.context = context;
         JCMediaManager.instance();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         if(viewType==TYPE_AD){
              View view=LayoutInflater.from(context).inflate(R.layout.fragment_recommand_type_ad,parent,false);
              RecomADHolder recomADHolder=new RecomADHolder(view);
              return  recomADHolder;
         }
         else  if(viewType==TYPE_1){
              View view= LayoutInflater.from(context).inflate(R.layout.fragment_recommand_type_1,parent,false);
              RecomType1Holder recomType1Holder=new RecomType1Holder(view);
              return recomType1Holder;
          }else{
              View view= LayoutInflater.from(context).inflate(R.layout.fragment_recommand_type_2,parent,false);
              RecomType2Holder recomType2Holder=new RecomType2Holder(view);
              return recomType2Holder;
          }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        if(type==TYPE_AD){
            RecomADHolder holder_AD = (RecomADHolder) holder;
            List<RecommendADEntity> list = recommendEntity.getmRecommendAD();
             holder_AD.covenientBanner.setPages(new CBViewHolderCreator(){
                 @Override
                 public Object createHolder() {
                     return new LocalImageHolderView();
                 }
             },list.subList(0,5)).setPointViewVisible(true)
                     .setPageIndicator(new int[]{R.drawable.do_gray_point,R.drawable.do_white_point})
                     .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
                     .startTurning(5000);

            calendar=Calendar.getInstance();
            int AM_PM = calendar.get(Calendar.AM_PM);
            if(AM_PM==Calendar.AM)
                holder_AD.textView.setText("— 上午好 —");
            else
                holder_AD.textView.setText("— 下午好 —");
        }
        if(type==TYPE_1){
            RecomType1Holder holder_1 = (RecomType1Holder) holder;
            RecommendInfo1Entity entity_1 = (RecommendInfo1Entity) recommendEntity.getmInfo().get(position-1 );
            holder_1.tv_title.setText(entity_1.getObject().getTitle());
            holder_1.tv_NaD.setText(entity_1.getObject().getArticleSource().getNickname());
            Picasso.with(context).load(entity_1.getObject().getImgUrl()).into(holder_1.iv);

        }
        if(type==TYPE_2){
            final RecomType2Holder holder_2= (RecomType2Holder) holder;
            RecommendInfo2Entity entity_2 = (RecommendInfo2Entity) recommendEntity.getmInfo().get(position - 1);
            holder_2.tv_type2_title.setText(entity_2.getObject().getTitle());
            holder_2.tv_type2_content.setText(entity_2.getObject().getDescription());
            holder_2.vv.setUp(entity_2.getObject().getVideoUrl(), JCVideoPlayer.SCREEN_LAYOUT_NORMAL,"",false);
            holder_2.vv.backButton.setVisibility(View.GONE);
            holder_2.vv.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            Glide.with(context).load(entity_2.getObject().getCoverUrl().split("@")[0]).into(holder_2.vv.thumbImageView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return TYPE_AD;
        }else {
            RecommendImpl recommend = recommendEntity.getmInfo().get(position-1);
            if(recommend instanceof RecommendInfo1Entity) {
                return TYPE_1;
            }
            else
                return TYPE_2;
        }
    }

    @Override
    public int getItemCount() {
        //内容加上一个广告区
        return recommendEntity.getmInfo()==null?1:recommendEntity.getmInfo().size()+1;
    }


    class RecomADHolder extends RecyclerView.ViewHolder {
        private  TextView textView;
        private ConvenientBanner covenientBanner;
        public RecomADHolder(View itemView) {
            super(itemView);
            textView= (TextView) itemView.findViewById(R.id.fg_recommend_AD_tv);
            covenientBanner= (ConvenientBanner) itemView.findViewById(R.id.fg_recommend_AD_vp);
           }
    }
    class RecomType1Holder extends RecyclerView.ViewHolder {
        private TextView tv_title;
        private TextView tv_NaD;
        private ImageView iv;
        public RecomType1Holder(View itemView) {
            super(itemView);
            tv_title= (TextView) itemView.findViewById(R.id.fg_recommend_type1_tv_title);
            tv_NaD= (TextView) itemView.findViewById(R.id.fg_recommend_type1_tv_NaD);
            iv= (ImageView) itemView.findViewById(R.id.fg_recommend_type1_image);
        }
    }
    class RecomType2Holder extends RecyclerView.ViewHolder {
        private JCVideoPlayerStandard vv;
        private TextView tv_type2_title;
        private TextView tv_type2_content;
        public RecomType2Holder(View itemView) {
            super(itemView);
            vv= (JCVideoPlayerStandard) itemView.findViewById(R.id.fg_recommend_type2_vv);
            tv_type2_title= (TextView) itemView.findViewById(R.id.fg_recommend_type2_title);
            tv_type2_content= (TextView) itemView.findViewById(R.id.fg_recommend_type2_content);
        }
    }
    class LocalImageHolderView implements Holder<RecommendADEntity>{
        private ImageView imageView;
        private TextView tv_title;
        private TextView tv_content;

        @Override
        public View createView(Context context) {
            View view= LayoutInflater.from(context).inflate(R.layout.fragment_recommmand_type_ad_vp_item,null);
            imageView= (ImageView) view.findViewById(R.id.fg_recommend_AD_vp_item);
            tv_title= (TextView) view.findViewById(R.id.fg_recommend_AD_vp_title);
            tv_content = (TextView) view.findViewById(R.id.fg_recommend_AD_vp_content);
            return view;
        }

        @Override
        public void UpdateUI(Context context, int position, RecommendADEntity data) {
            Picasso.with(context).load(data.getImageUrl()).into(imageView);
            tv_title.setText(data.getMainTitle());
            tv_content.setText(data.getSubTile());
        }


    }
}
